<?php
/**
 * 文档中心(营销资料库，公司制度)
 * Enter description here ...
 * @author cc
 *
 */
namespace Home\Controller;
use Think\Controller;
class DatabaseController extends CommonController{
	public function way(){
		session("action","Database");
		session("menu_id",I('get.menu_id'));
		$this->redirect("Public/index");
	}
	
	public function index(){
		$this->redirect("folder");
	}
	
	public function marketing(){
		$catalogue = M("ziliao_catalogue");
		$data = $catalogue->where('tpl=1')->order('id desc')->select();
		$this->assign('data',$data);
		$this->assign("empty",emptyhtml('html'));
		$this->display();
	}
	
	public function marketing_upload(){
		$catalogue = M("ziliao_catalogue");
		$data = $catalogue->where('tpl=1')->order('id desc')->select();
		$this->assign('data',$data);
		$this->assign("empty",emptyhtml('html'));
		$this->assign("type", "1");
		$this->display();
	}
	
	public function rule(){
		$catalogue = M("ziliao_catalogue");
		$data = $catalogue->where('tpl=2')->order('id desc')->select();
		$this->assign('data',$data);
		$this->assign("empty",emptyhtml('html'));
		$this->display();
	}
	
	public function rule_upload(){
		$catalogue = M("ziliao_catalogue");
		$data = $catalogue->where('tpl=2')->order('id desc')->select();
		$this->assign('data',$data);
		$this->assign("type", "2");
		$this->assign("empty",emptyhtml('html'));
		$this->display();
	}
	
	public function lists(){
		$catalogue_id = I('get.id');
		$type = I('get.type');
		if(!is_id($catalogue_id)){ $this->error("非法参数！"); }
		if($type != "1" && $type != '2'){ $this->error("非法参数！"); }
		$ziliao = M("ziliao");
		$user = M("sys_user");
		$data_list = $ziliao->where('tpl=%d',$catalogue_id)->select();
		$this->assign('data_list',$data_list);
		$this->assign("type",$type);
		$this->assign("empty",emptyhtml('list'));
		$this->display();
	}
	
	public function upload(){
		$catalogue = M("ziliao_catalogue");
		$id = I('get.id');
		$type = I('get.type');
		if(!is_id($id)){ $this->error("非法参数！"); }
		if($type != "1" && $type != '2'){ $this->error("非法参数！"); }
		$foldername = $catalogue->where("id=%d",$id)->getField("name");
		$this->assign('id',$id);
		$this->assign("type",$type);
		$this->assign('foldername',$foldername);
		$this->display();
	}
	
	public function uploadfile(){
		$id = I('get.id');//目录的id
		$type = I('get.type');
		if(!is_id($id)){ $this->error("非法参数！"); }
		if($type != "1" && $type != '2'){ $this->error("非法参数！"); }
		
		$catalogue = M("ziliao_catalogue");
		$alias = $catalogue->where("id=%d",$id)->getField("alias");
		if($type == '1'){
			$targetFolder = C('PUBLIC_PATH').'upload/marketing/'.$alias; 
		}elseif($type == '2'){
			$targetFolder = C('PUBLIC_PATH').'upload/rule/'.$alias; 
		}
		$verifyToken = md5('unique_salt' . $_POST['timestamp']);
		if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
			$tempFile = $_FILES['Filedata']['tmp_name'];
			$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
			if(!is_dir($targetPath)){
				mkdir($targetPath, 0777, true);
			}
			$filename = $_FILES['Filedata']['name'];
			$fileParts = pathinfo($filename);
			$extension = $fileParts['extension'];
			
			// Validate the file type
			$fileTypes = array('xlsx','xls','csv','txt','ppt','doc','docx','pdf'); // File extensions
			$targetFile = rtrim($targetPath,'/') . '/' . $filename;
			$targetFile = iconv("UTF-8", "GB2312", $targetFile);
			if (in_array($extension,$fileTypes)) {
				move_uploaded_file($tempFile,$targetFile);
				$model = M("ziliao");
				$data = array(
					'name' => $filename,
					'path' => $targetFolder.'/'.$filename,
					'author' => session('user_name'),
					'tpl' => $id,
					'upload_time' => date("Y-m-d H:i:s"),
					'type' => $type,
					'status' => 0,
				);
				$success = $model->add($data);
				if($success){
					$this->systemlog("upload", "资料上传", "上传资料---".$filename);
					$this->ajaxReturn("上传成功！");
				}else{
					$this->ajaxReturn("上传失败！");
				}
			}else{
				$this->ajaxReturn("上传的文件格式不被允许！");
			}
		}	
	}
	
	public function upload_list(){
		$catalogue_id = I('get.id');
		$type = I('get.type');
		if(!is_id($catalogue_id)){ $this->error("非法参数！"); }
		if($type != "1" && $type != '2'){ $this->error("非法参数！"); }
		
		$ziliao = M("ziliao");
		$catalogue = M("ziliao_catalogue");
		$foldername = $catalogue->where("id=%d",$catalogue_id)->getField("name");
		$data_list = $ziliao->where('tpl=%d',$catalogue_id)->select();
		$this->assign('data_list',$data_list);
		$this->assign('id',$catalogue_id);
		$this->assign('type',$type);
		$this->assign('foldername',$foldername);
		$this->assign("empty",emptyhtml('list'));
		$this->display();
		
	}
	
	public function add(){//新建文件夹
		if(!IS_POST){ $this->error("非法参数！");exit; }
		$catalogue = M("ziliao_catalogue");
		$data = $_POST;
		$data['alias'] = date('YmdHis');
		$tpl = I('post.tpl');
		if($tpl != "1" && $tpl != '2'){ $this->error("非法参数！"); }
		
		if($tpl == '1'){
			$targetFolder = C('PUBLIC_PATH').'upload/marketing/'.$data['alias'];
		}elseif($tpl == '2'){
			$targetFolder = C('PUBLIC_PATH').'upload/rule/'.$data['alias']; 
		}
		$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
		if(!is_dir($targetPath)){
			mkdir($targetPath, 0777, true);
		}
		$success = $catalogue->data($data)->add();
		if($success){
			$this->systemlog("add", "资料库", "新建文件夹---".$data['name']);
		}
	}
	
	public function folderdel(){//删除文件夹
		$catalogue_id = I('get.id');
		$type = I('get.type');
		if(!is_id($catalogue_id)){ $this->error("非法参数！"); }
		if($type != "1" && $type != '2'){ $this->error("非法参数！"); }
		$ziliao = M("ziliao");
		$catalogue = M("ziliao_catalogue");
		$data = $ziliao->where("tpl=%d",$catalogue_id)->select();
		if(count($data) > 0){
			foreach ($data as $value){
				$path = $_SERVER['DOCUMENT_ROOT'] . $value['path'];
				$path = iconv("UTF-8", "GB2312", $path);
				if(is_file($path)){
					unlink($path);
					$ziliao->where("id=%d",$value['id'])->delete();
				}
			}
		}
		$name = $catalogue->where("id=%d",$catalogue_id)->getField("name");
		$success = $catalogue->where("id=%d",$catalogue_id)->delete();
		if($success){
			$this->systemlog("delete", "资料库", "删除文件夹---".$data['name']."--及该文件夹下的文件！");
		}
	}
	
	public function delete(){
		$id = I('get.id');
		if(!is_id($id)){ $this->error("非法参数！"); }
		$ziliao = M("ziliao");
		$path = $ziliao->where("id=%d",$id)->getField('path');
		$path = $_SERVER['DOCUMENT_ROOT'] . $path;
		$path = iconv("UTF-8", "GB2312", $path);
		if(is_file($path)){
			unlink($path);
		}
		$name = $ziliao->where("id=%d",$id)->getField("name");
		$success = $ziliao->where("id=%d",$id)->delete();
		if($success){
			$this->systemlog("delete", "资料库", "删除资料---".$name);
			$this->ajaxReturn("删除成功！");
		}else{
			$this->ajaxReturn("删除失败！");
		}
	}
	
	public function download(){
		$id = I('get.id');
		$type = I('get.type');
		if(!is_id($id)){ $this->error("非法参数！"); }
		if($type != "1" && $type != '2'){ $this->error("非法参数！"); }
		if($type == '1'){
			if(in_array("41", session("menu"))){//判断是否有权限下载
				//开始下载
				$this->filedown($id, $type);
			}else{
				$this->downerror("对不起你无权限下载资料！");
			}
		}else{
			//开始下载
			$this->filedown($id, $type);
		}
	}
	
	function filedown($id, $type){
		$ziliao = M("ziliao");
		$data = $ziliao->where("id=%d and type=%d",$id,$type)->find();
		if(empty($data)){
			$this->downerror("数据异常！");
		}
		$filename = $data['name'];
		$path = $data['path'];
		$path = $_SERVER['DOCUMENT_ROOT'] . $path;
		$path = iconv("UTF-8", "GB2312", $path);
		if(!is_file($path)){
			$this->downerror("文件不存在！");
		}
		
		$file = fopen($path,"r"); // 打开文件
		// 输入文件标签
		Header("Content-type: application/octet-stream");
		Header("Accept-Ranges: bytes");
		Header("Accept-Length: ".filesize($path));
		Header("Content-Disposition: attachment; filename=" . iconv("UTF-8", "GB2312", $filename));
		// 输出文件内容
		echo fread($file,filesize($path));
		fclose($file);
		exit();
	}
	
	function downerror($msg){
		echo "<script language='javascript'>alert('$msg')</script>";
		echo "<script language='javascript'>window.setTimeout(window.close(),3000)</script>";
		exit();
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
	
}
?>